﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
using System.IO;
using SABnzbdNET.ServiceAgents;

namespace SABnzbdNET.Dialogs
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Main : Window
    {
        private WindowState storedWindowState = WindowState.Normal;

        public string StatusMessage { set { textError.Text = value; } }
        public string NotifyIconTooltip { set { notifyIcon.BalloonTipText = value; } }

        public Main()
        {
            InitializeComponent();
            CheckTrayIcon();

            notifyIcon.BalloonTipText = "SABnzbdNET has been minimised. Click the tray icon to show.";
            notifyIcon.BalloonTipTitle = "SABnzbdNET";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
                Hide();
        }

        private void OnStateChanged(object sender, EventArgs args)
        {
            if (WindowState == WindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
                Hide();
            else
                storedWindowState = WindowState;
        }

        private void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs args)
        {
            CheckTrayIcon();
        }

        private void menuRestore_Click(object sender, RoutedEventArgs e)
        {
            RestoreWindow();
        }

        private void menuUpdate_Click(object sender, RoutedEventArgs e)
        {
            UpdateServerStatus();
            notifyIcon.ShowBalloonTip(1000);
        }

        private void menuClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnThumbPause_Click(object sender, EventArgs e)
        {
            new ServerAgent().PauseServer(UpdateServerStatus);
        }

        private void btnThumbRefresh_Click(object sender, EventArgs e)
        {
            UpdateServerStatus();
        }

        private void btnThumbStart_Click(object sender, EventArgs e)
        {
            new ServerAgent().StartServer(UpdateServerStatus);
        }

        private void notifyIcon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            notifyIcon.ShowBalloonTip(1000);
        }

        private void CheckTrayIcon()
        {
            notifyIcon.Visibility = ((IsVisible && Properties.Settings.Default.MinimizeToTray) || !Properties.Settings.Default.MinimizeToTray) ? Visibility.Collapsed : Visibility.Hidden;
        }

        private void RestoreWindow()
        {
            Show();
            UpdateServerStatus();
            WindowState = storedWindowState;

        }

        public void UpdateServerStatus()
        {
            ServerStatus.Update();
        }
    }
}
